#include "gprsfactory.h"

CGPRSFactory::CGPRSFactory()
{

}

CGPRSFactory::~CGPRSFactory()
{

}

//  1 - 解析成功 - 不需要回复
//  0 - 解析成功
// -1 - 参数错误
// -2 - 数据长度不对
// -3 - CRC校验错误  
// -4 - 数据区长度不对
// -5 - 获取数据失败
// -6 - 命令号不正确
// -7 - 命令执行失败
// -999 - 其他错误
int CGPRSFactory::analyDataDatagramStack(SGPRSHead* pGPRSHead,  CBattStack* pBattStack, char* pData, int iLen)
{
	if(!pGPRSHead || !pBattStack || !pData)
	{
		return -1;
	}

	if(pGPRSHead->usDataLen != iLen - sizeof(SGPRSHead) - 2/*CRC*/)
	{
		return -2;
	}

	// some variables
	int iRetVal = -999;	
	SNetBattStack* pNetBattStack = 0;

	quint16 usCRC = 0;
	quint16 usCRC1 = 0;

	// Check CRC
	usCRC = CRC16((uchar*)pData, iLen - 2/*CRC*/);
	usCRC1 = 0;
	getParamFromBuffer(pData + iLen - 2/*CRC*/, usCRC1);
	if (usCRC1 == usCRC)
	{
		if (GPRS_DATA_STACK == pGPRSHead->uCmdType) // 电池堆
		{
			if (pGPRSHead->usDataLen == sizeof(SNetBattStack))
			{
				pNetBattStack = (SNetBattStack*)(pData + sizeof(SGPRSHead));
				if(pNetBattStack)
				{
					// Data
					pBattStack->usStackState = pNetBattStack->usStackState;                  // 电池堆状态            1   
					pBattStack->usBranParalState1 = pNetBattStack->usBranParalState1;        // 第1支路并联状态     18  
					pBattStack->usBranParalState2 = pNetBattStack->usBranParalState2; 	     // 第2支路并联状态      19
					pBattStack->usBranParalState3 = pNetBattStack->usBranParalState3; 	     // 第3支路并联状态      20
					pBattStack->usBranParalState4 = pNetBattStack->usBranParalState4; 	     // 第4支路并联状态      21
					pBattStack->usBranState1 = pNetBattStack->usBranState1;                  // 第1支路电池堆状态    38         
					pBattStack->usBranState2 = pNetBattStack->usBranState2;                  // 第2支路电池堆状态    39          
					pBattStack->usBranState3 = pNetBattStack->usBranState3;                  // 第3支路电池堆状态    40         
					pBattStack->usBranState4 = pNetBattStack->usBranState4;                  // 第4支路电池堆状态    41			

					pBattStack->usStackStateFull = pNetBattStack->usStackStateFull;		     // 电池堆充满           44
					pBattStack->usStackStateEmpty = pNetBattStack->usStackStateEmpty;		 // 电池堆放空           45
					pBattStack->usStackStateTempAlarm = pNetBattStack->usStackStateTempAlarm;// 电池堆温度告警       46
					pBattStack->usStackStateCurAlarm = pNetBattStack->usStackStateCurAlarm;  // 电池堆电流告警       47
					pBattStack->usStackStateAllOff = pNetBattStack->usStackStateAllOff;		 // 电池堆接触器全断开   48
					pBattStack->usStackStateFault = pNetBattStack->usStackStateFault;		 // 电池堆保护(PCS故障)  49

					pBattStack->fStackSoc = pNetBattStack->fStackSoc;                        // 电池堆SOC             2
					pBattStack->fStackSoh = pNetBattStack->fStackSoh; 	                     // 电池堆SOH             4
					pBattStack->fStackChargeEQ = pNetBattStack->fStackChargeEQ; 	         // 电池堆可充电量        6
					pBattStack->fStackDischargeEQ = pNetBattStack->fStackDischargeEQ;        // 电池堆可放电量        8
					pBattStack->fStackChargeEQAdd = pNetBattStack->fStackChargeEQAdd;        // 电池堆充电量累加值    10
					pBattStack->fStackDischargeEQAdd = pNetBattStack->fStackDischargeEQAdd;  // 电池堆放电量累加值    12

					pBattStack->fStackTotalVol = pNetBattStack->fStackTotalVol;              // 电池堆总电压         14  
					pBattStack->fStackTotalCur = pNetBattStack->fStackTotalCur; 	         // 电池堆总电流         16

					pBattStack->fBranSoc1 = pNetBattStack->fBranSoc1; 	             // 第1支路电池SOC       22
					pBattStack->fBranSoc2 = pNetBattStack->fBranSoc2; 	             // 第2支路电池SOC       24
					pBattStack->fBranSoc3 = pNetBattStack->fBranSoc3; 	             // 第3支路电池SOC       26
					pBattStack->fBranSoc4 = pNetBattStack->fBranSoc4; 	             // 第4支路电池SOC       28
					pBattStack->fBranCur1 = pNetBattStack->fBranCur1; 	             // 第1支路当前电流      30
					pBattStack->fBranCur2 = pNetBattStack->fBranCur2; 	             // 第2支路当前电流      32
					pBattStack->fBranCur3 = pNetBattStack->fBranCur3; 	             // 第3支路当前电流      34
					pBattStack->fBranCur4 =pNetBattStack->fBranCur4 ; 	             // 第4支路当前电流      36

					pBattStack->fSglTempCompVol = pNetBattStack->fSglTempCompVol;	 // 单体电池温度补偿电压 42	

					iRetVal = 0;
				}
				else
				{
					iRetVal = -5;
				}
			}
			else
			{
				iRetVal = -4;
			}
		}
		else
		{
			iRetVal = -6;
		}
	}
	else
	{
		iRetVal = -3;
	}

	return iRetVal;
}

//  1 - 解析成功 - 不需要回复
//  0 - 解析成功
// -1 - 参数错误
// -2 - 数据长度不对
// -3 - CRC校验错误  
// -4 - 数据区长度不对
// -5 - 获取数据失败
// -6 - 命令号不正确
// -7 - 命令执行失败
// -999 - 其他错误
int CGPRSFactory::analyDataDatagramBank(SGPRSHead* pGPRSHead, CBattBank* pBattBank, CBankAlarm* pBankAlarm, char* pData, int iLen)
{
	if(!pGPRSHead || !pBattBank || !pData || !pBankAlarm)
	{
		return -1;
	}

	if(pGPRSHead->usDataLen != iLen - sizeof(SGPRSHead) - 2/*CRC*/)
	{
		return -2;
	}

	// some variables
	int iRetVal = -999;	
	SNetBattBankAlarm* pNetBattBankAlarm = 0;
	SNetBattBankSglOther* pNetBattBankSglOther = 0;

	float* pFVal = 0;
	quint16 usCRC = 0;
	quint16 usCRC1 = 0;

	// Check CRC
	usCRC = CRC16((uchar*)pData, iLen - 2/*CRC*/);
	usCRC1 = 0;
	getParamFromBuffer(pData + iLen - 2/*CRC*/, usCRC1);
	if (usCRC1 == usCRC)
	{
		if (GPRS_DATA_BANK_ALARM == pGPRSHead->uCmdType)  // 电池组数据 - 告警信息 
		{
			if (pGPRSHead->usDataLen == sizeof(SNetBattBankAlarm))
			{
				pNetBattBankAlarm = (SNetBattBankAlarm*)(pData + sizeof(SGPRSHead));
				if (pNetBattBankAlarm)
				{
					pBattBank->aSglVolUpLimitAlarm = pNetBattBankAlarm->aSglVolUpLimitAlarm;		 // 单体电压过高告警       1
					pBattBank->aSglVolDownLimitAlarm = pNetBattBankAlarm->aSglVolDownLimitAlarm;	 // 单体电压过低告警       2	
					pBattBank->aSglTempUpLimitAlarm = pNetBattBankAlarm->aSglTempUpLimitAlarm;		 // 单体温度过高告警	   3
					pBattBank->aSglTempDownLimitAlarm = pNetBattBankAlarm->aSglTempDownLimitAlarm;	 // 单体温度过低告警	   4
					pBattBank->aSglVolUpLimitProt = pNetBattBankAlarm->aSglVolUpLimitProt;		     // 单体电压过高保护	   5
					pBattBank->aSglVolDownLimitProt = pNetBattBankAlarm->aSglVolDownLimitProt;		 // 单体电压过低保护	   6	
					pBattBank->aSglTempUpLimitProt = pNetBattBankAlarm->aSglTempUpLimitProt;		 // 单体温度过高保护	   7
					pBattBank->aSglTempDownLimitProt = pNetBattBankAlarm->aSglTempDownLimitProt;	 // 单体温度过低保护	   8	
					pBattBank->aBankVolUpLimitAlarm = pNetBattBankAlarm->aBankVolUpLimitAlarm;	     // 组电压过高告警         9
					pBattBank->aBankVolDownLimitAlarm = pNetBattBankAlarm->aBankVolDownLimitAlarm;   // 组电压过低告警         10
					pBattBank->aBankCurUpLimitAlarm = pNetBattBankAlarm->aBankCurUpLimitAlarm;		 // 组电流过高告警         11
					pBattBank->aEnvTempUpLimitAlarm = pNetBattBankAlarm->aEnvTempUpLimitAlarm;		 // 环境温度过高告警	   12
					pBattBank->aEnvTempDownLimitAlarm = pNetBattBankAlarm->aEnvTempDownLimitAlarm;	 // 环境温度过低告警	   13
					pBattBank->aBankSocDownLimitAlarm = pNetBattBankAlarm->aBankSocDownLimitAlarm;	 // SOC超下限告警		   14	
					pBattBank->aBankVolUpLimitProt = pNetBattBankAlarm->aBankVolUpLimitProt;		 // 组电压过高保护	       15
					pBattBank->aBankVolDownLimitProt = pNetBattBankAlarm->aBankVolDownLimitProt;	 // 组电压过低保护	       16	
					pBattBank->aBankCurUpLimitProt = pNetBattBankAlarm->aBankCurUpLimitProt;		 // 组电流过高保护		   17
					pBattBank->aEnvTempUpLimitProt = pNetBattBankAlarm->aEnvTempUpLimitProt;		 // 环境温度过高保护	   18
					pBattBank->aEnvTempDownLimitProt = pNetBattBankAlarm->aEnvTempDownLimitProt;	 // 环境温度过低保护	   19	
					pBattBank->aBankSocDownLimitProt = pNetBattBankAlarm->aBankSocDownLimitProt;	 // SOC超下限保护		   20
					pBattBank->aBMUConnFaultAlarm = pNetBattBankAlarm->aBMUConnFaultAlarm;		     // BMU设备连接故障告警	   21
					pBattBank->aBCMUConnFaultAlarm = pNetBattBankAlarm->aBCMUConnFaultAlarm;		 // BCMU设备连接故障告警   22	
					pBattBank->aSglVolUpLimitWarn = pNetBattBankAlarm->aSglVolUpLimitWarn;	         // 单体电压过高预警	   23
					pBattBank->aSglVolDownLimitWarn = pNetBattBankAlarm->aSglVolDownLimitWarn;	     // 单体电压过低预警	   24	
					pBattBank->aSglTempUpLimitWarn = pNetBattBankAlarm->aSglTempUpLimitWarn;	     // 单体温度过高预警	   25	
					pBattBank->aSglTempDownLimitWarn = pNetBattBankAlarm->aSglTempDownLimitWarn;	 // 单体温度过低预警       26
					pBattBank->aBankSocUpLimitAlarm = pNetBattBankAlarm->aBankSocUpLimitAlarm;	     // SOC超上限告警		   27
					pBattBank->aBankSocUpLimitProt = pNetBattBankAlarm->aBankSocUpLimitProt;		 // SOC超上限保护		   28
					pBattBank->aPosResDwonLimitProt = pNetBattBankAlarm->aPosResDwonLimitProt;		 // 正极绝缘内阻下限保护   29
					pBattBank->aNegResDwonLimitProt = pNetBattBankAlarm->aNegResDwonLimitProt;		 // 负极绝缘内阻下限保护   30
					pBattBank->aPackVolDiffUpLimitAlarm = pNetBattBankAlarm->aPackVolDiffUpLimitAlarm;	 // 箱压差上限告警         31
					pBattBank->aPackVolDiffUpLimitProt = pNetBattBankAlarm->aPackVolDiffUpLimitProt;	 // 箱压差上限保护         32
					pBattBank->aPackTempDiffUpLimitAlarm = pNetBattBankAlarm->aPackTempDiffUpLimitAlarm; // 箱温差上限告警         33
					pBattBank->aPackTempDiffUpLimitProt = pNetBattBankAlarm->aPackTempDiffUpLimitProt;	 // 箱温差上限保护         34	
					pBattBank->aBankVolUpLimitWarn = pNetBattBankAlarm->aBankVolUpLimitWarn;	         // 组电压过高预警         35
					pBattBank->aBankVolDownLimitWarn = pNetBattBankAlarm->aBankVolDownLimitWarn;         // 组电压过低预警         36
					pBattBank->aBankCurUpLimitWarn = pNetBattBankAlarm->aBankCurUpLimitWarn;		     // 组电流过高预警         37
					pBattBank->aEnvTempUpLimitWarn = pNetBattBankAlarm->aEnvTempUpLimitWarn;		     // 环境温度过高预警	   38
					pBattBank->aEnvTempDownLimitWarn = pNetBattBankAlarm->aEnvTempDownLimitWarn;	     // 环境温度过低预警	   39
					pBattBank->aBankSocUpLimitWarn = pNetBattBankAlarm->aBankSocUpLimitWarn;	         // SOC超上限预警		   40
					pBattBank->aBankSocDownLimitWarn = pNetBattBankAlarm->aBankSocDownLimitWarn;	     // SOC超下限预警		   41
					pBattBank->aPackVolDiffUpLimitWarn = pNetBattBankAlarm->aPackVolDiffUpLimitWarn;	 // 箱压差上限预警         42
					pBattBank->aPackTempDiffUpLimitWarn = pNetBattBankAlarm->aPackTempDiffUpLimitWarn;	 // 箱温差上限预警         43
					pBattBank->aBkwdSglVolReptProt = pNetBattBankAlarm->aBkwdSglVolReptProt;             // 落后电池电压多次保护   44
					pBattBank->aSglVolWireFault = pNetBattBankAlarm->aSglVolWireFault;                   // 单体电压采集线故障     45
					pBattBank->aBankVolWireFault = pNetBattBankAlarm->aBankVolWireFault;                 // 总压采集线故障         46
					pBattBank->aCurWireFault = pNetBattBankAlarm->aCurWireFault;                         // 电流采集线故障         47
					pBattBank->aTempWireBreak = pNetBattBankAlarm->aTempWireBreak;                       // 温度采集断线           48
					pBattBank->aTempShortCircuit = pNetBattBankAlarm->aTempShortCircuit;                 // 温度采集短路           49	
					pBattBank->aBMSFault = pNetBattBankAlarm->aBMSFault;                                 // BMS设备故障            50	
					pBattBank->aBankCurAbnormity = pNetBattBankAlarm->aBankCurAbnormity;                 // 组电流异常             51
					pBattBank->aAlarmAvailable = pNetBattBankAlarm->aAlarmAvailable;                     // 告警信息待获取         52
     
					iRetVal = 0;					
				}
				else
				{
					iRetVal = -5;
				}
			}
			else
			{
				iRetVal = -4;
			}
		}
		else if (GPRS_DATA_BANK_SGL_VOL1 == pGPRSHead->uCmdType)  // 单体电压   1 - 100
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglVol[i] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}			
		}
		else if (GPRS_DATA_BANK_SGL_VOL2 == pGPRSHead->uCmdType)  // 单体电压 101 - 200
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglVol[i + 100] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}
		else if (GPRS_DATA_BANK_SGL_VOL3 == pGPRSHead->uCmdType)  // 单体电压 201 - 300
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglVol[i + 200] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}
		else if (GPRS_DATA_BANK_SGL_VOL4 == pGPRSHead->uCmdType)  // 单体电压 301 - 400
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglVol[i + 300] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}
		else if (GPRS_DATA_BANK_SGL_TEMP1 == pGPRSHead->uCmdType)  // 单体温度   1 - 100
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglTemp[i] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}
		else if (GPRS_DATA_BANK_SGL_TEMP2 == pGPRSHead->uCmdType)  // 单体温度 101 - 200
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglTemp[i + 100] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}
		else if (GPRS_DATA_BANK_SGL_TEMP3 == pGPRSHead->uCmdType)  // 单体温度 201 - 300
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglTemp[i + 200] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}	
		else if (GPRS_DATA_BANK_SGL_TEMP4 == pGPRSHead->uCmdType)  // 单体温度 301 - 400
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglTemp[i + 300] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}
		else if (GPRS_DATA_BANK_SGL_SOC1 == pGPRSHead->uCmdType)  // 单体Soc   1 - 100
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglSoc[i] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}
		else if (GPRS_DATA_BANK_SGL_SOC2 == pGPRSHead->uCmdType)  // 单体Soc 101 - 200
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglSoc[i + 100] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}
		else if (GPRS_DATA_BANK_SGL_SOC3 == pGPRSHead->uCmdType)  // 单体Soc 201 - 300
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglSoc[i + 200] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}
		else if (GPRS_DATA_BANK_SGL_SOC4 == pGPRSHead->uCmdType)  // 单体Soc 301 - 400
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglSoc[i + 300] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}
		else if (GPRS_DATA_BANK_SGL_RES1 == pGPRSHead->uCmdType)  // 单体内阻   1 - 100
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglRes[i] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}
		else if (GPRS_DATA_BANK_SGL_RES2 == pGPRSHead->uCmdType)  // 单体内阻 101 - 200
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglRes[i + 100] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}
		else if (GPRS_DATA_BANK_SGL_RES3 == pGPRSHead->uCmdType)  // 单体内阻 201 - 300
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglRes[i + 200] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}
		else if (GPRS_DATA_BANK_SGL_RES4 == pGPRSHead->uCmdType)  // 单体内阻 301 - 400
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglRes[i + 300] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}
		else if (GPRS_DATA_BANK_SGL_SOH1 == pGPRSHead->uCmdType)  // 单体Soh   1 - 100
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglSoh[i] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}
		else if (GPRS_DATA_BANK_SGL_SOH2 == pGPRSHead->uCmdType)  // 单体Soh 101 - 200
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglSoh[i + 100] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}
		else if (GPRS_DATA_BANK_SGL_SOH3 == pGPRSHead->uCmdType)  // 单体Soh 201 - 300
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglSoh[i + 200] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}
		else if (GPRS_DATA_BANK_SGL_SOH4 == pGPRSHead->uCmdType)  // 单体Soh 301 - 400
		{
			if (pGPRSHead->usDataLen == 4 * 100)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				for (int i = 0; i < 100; ++i)
				{
					pBattBank->fSglSoh[i + 300] = *pFVal;
					++pFVal;
				}

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}	
		}
		else if (GPRS_DATA_BANK_OTHER == pGPRSHead->uCmdType)  // 其他电池组数据
		{
			if (pGPRSHead->usDataLen == sizeof(SNetBattBankSglOther))
			{
				pNetBattBankSglOther = (SNetBattBankSglOther*)(pData + sizeof(SGPRSHead));
				if (pNetBattBankSglOther)
				{
					// Data
					pBattBank->usSglMaxVolNo = pNetBattBankSglOther->usSglMaxVolNo;           // 单电池最大电压对应ID号 5758
					pBattBank->usSglMinVolNo = pNetBattBankSglOther->usSglMinVolNo;           // 单电池最小电压对应ID号 5761
					pBattBank->usSglMaxTempNo = pNetBattBankSglOther->usSglMaxTempNo;         // 单电池最大温度对应ID号 5764
					pBattBank->usSglMinTempNo = pNetBattBankSglOther->usSglMinTempNo;         // 单电池最小温度对应ID号 5767
					pBattBank->usSglMaxSocNo = pNetBattBankSglOther->usSglMaxSocNo;           // 单电池最大SOC对应ID号  5770	
					pBattBank->usSglMinSocNo = pNetBattBankSglOther->usSglMinSocNo;           // 单电池最小SOC对应ID号  5773
					pBattBank->usSglMaxSohNo = pNetBattBankSglOther->usSglMaxSohNo;           // 单电池最大SOH对应ID号  5776
					pBattBank->usSglMinSohNo = pNetBattBankSglOther->usSglMinSohNo;           // 单电池最小SOH对应ID号  5779
					pBattBank->usSglMaxResNo = pNetBattBankSglOther->usSglMaxResNo;           // 单电池最大内阻对应ID号 5786
					pBattBank->usSglMinResNo = pNetBattBankSglOther->usSglMinResNo;           // 单电池最小内阻对应ID号 5789
					pBattBank->usBCMUInput1 = pNetBattBankSglOther->usBCMUInput1;             // BCMU开关状态寄存器Input 1（直流接触器状态）		6000  1--闭合， 0--断开
					pBattBank->usBCMUInput2 = pNetBattBankSglOther->usBCMUInput2; 	          // BCMU Input 2（熔断器1状态）                    	6014
					pBattBank->usBCMUInput3 = pNetBattBankSglOther->usBCMUInput3; 	          // BCMU Input 3（熔断器2状态）	                    6016
					pBattBank->usBCMUInput4 = pNetBattBankSglOther->usBCMUInput4; 	          // BCMU Input 4（手动空开状态）		            6018			
					pBattBank->usBankState = pNetBattBankSglOther->usBankState; 
					pBattBank->usBankStatePCS = pNetBattBankSglOther->usBankStatePCS; 	      // 电池组状态（告知PCS）	                        6100  0x1111充满，0x2222放空，0x5555待机，0xaaaa跳机，0xbbbb正常

					pBattBank->fSglAvgRes = pNetBattBankSglOther->fSglAvgRes;                 // 单体电池平均内阻	   5746
					pBattBank->fSglAvgSoh = pNetBattBankSglOther->fSglAvgSoh;                 // 单体电池SOH平均值      5748
					pBattBank->fSglAvgSoc = pNetBattBankSglOther->fSglAvgSoc; 	              // 单体电池SOC平均值      5750	
					pBattBank->fSglAvgTemp = pNetBattBankSglOther->fSglAvgTemp; 	          // 单体电池温度平均值     5752
					pBattBank->fSglAvgVol = pNetBattBankSglOther->fSglAvgVol;                 // 单体电池电压平均值     5754
					pBattBank->fSglMaxVol = pNetBattBankSglOther->fSglMaxVol;                 // 单电池电压最大值       5756
					pBattBank->fSglMinVol = pNetBattBankSglOther->fSglMinVol; 	              // 单电池电压最小值	   5759	
					pBattBank->fSglMaxTemp = pNetBattBankSglOther->fSglMaxTemp;               // 单电池温度最大值	   5762	
					pBattBank->fSglMinTemp = pNetBattBankSglOther->fSglMinTemp;               // 单电池温度最小值	   5765	
					pBattBank->fSglMaxSoc = pNetBattBankSglOther->fSglMaxSoc;                 // 单体SOC最大值		   5768	
					pBattBank->fSglMinSoc = pNetBattBankSglOther->fSglMinSoc;                 // 单体SOC最小值	       5771	
					pBattBank->fSglMaxSoh = pNetBattBankSglOther->fSglMaxSoh;                 // 单体SOH最大值	       5774	
					pBattBank->fSglMinSoh = pNetBattBankSglOther->fSglMinSoh;                 // 单体SOH最小值	       5777	
					pBattBank->fBankSoc = pNetBattBankSglOther->fBankSoc;                     // 组SOC		           5780
					pBattBank->fBankSoh = pNetBattBankSglOther->fBankSoh;                     // 组SOH	               5782
					pBattBank->fSglMaxRes = pNetBattBankSglOther->fSglMaxRes;                 // 单体内阻最大值	       5784	
					pBattBank->fSglMinRes = pNetBattBankSglOther->fSglMinRes;                 // 单体内阻最小值		   5787

					pBattBank->fBCMUBankVol = pNetBattBankSglOther->fBCMUBankVol; 	          // BCMU组端电压		                            6002
					pBattBank->fBCMUBankCur = pNetBattBankSglOther->fBCMUBankCur; 	          // BCMU组电流		                                6004
					pBattBank->fBCMUBankTemp = pNetBattBankSglOther->fBCMUBankTemp; 	      // BCMU环境温度		                            6006
					pBattBank->fBCMUPosRes = pNetBattBankSglOther->fBCMUPosRes; 	          // BCMU 正极绝缘电阻	                            6010
					pBattBank->fBCMUNegRes = pNetBattBankSglOther->fBCMUNegRes;               // BCMU 负极绝缘电阻	                         	6012

					pBattBank->fBankChargeEQ = pNetBattBankSglOther->fBankChargeEQ; 	      // 电池组可充电量                               	6200
					pBattBank->fBankDischargeEQ = pNetBattBankSglOther->fBankDischargeEQ; 	  // 电池组可放电量	                                6202                          
					pBattBank->fBankChargeEQAdd = pNetBattBankSglOther->fBankChargeEQAdd; 	         // 电池组累计充电电量	                            6216
					pBattBank->fBankDischargeEQAdd = pNetBattBankSglOther->fBankDischargeEQAdd;      // 电池组累计放电电量	                            6218
					pBattBank->fBankSglChargeEQAdd = pNetBattBankSglOther->fBankSglChargeEQAdd;   	 // 电池组单次累计充电电量	                        6220
					pBattBank->fBankSglDischargeEQAdd = pNetBattBankSglOther->fBankSglDischargeEQAdd;// 电池组单次累计放电电量	                        6222

					iRetVal = 0;
				}
				else
				{
					iRetVal = -5;
				}				
			}
			else
			{
				iRetVal = -4;
			}
		}
		else if (GPRS_DATA_WARN_PARAM == pGPRSHead->uCmdType)  // 读取预警参数
		{
			if (pGPRSHead->usDataLen == 4 * 12)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				pBankAlarm->param.fBankVolUpLimitWarn = *pFVal;
				++pFVal;
				pBankAlarm->param.fBankVolDownLimitWarn = *pFVal;
				++pFVal;
				pBankAlarm->param.fChargeCurUpLimitWarn = *pFVal;
				++pFVal;
				pBankAlarm->param.fDischargeCurUpLimitWarn = *pFVal;
				++pFVal;
				pBankAlarm->param.fSglVolUpLimitWarn = *pFVal;
				++pFVal;
				pBankAlarm->param.fSglVolDownLimitWarn = *pFVal;
				++pFVal;
				pBankAlarm->param.fChargeSglTempUpLimitWarn = *pFVal;
				++pFVal;
				pBankAlarm->param.fChargeSglTempDownLimitWarn = *pFVal;
				++pFVal;
				pBankAlarm->param.fDchargeSglTempUpLimitWarn = *pFVal;
				++pFVal;
				pBankAlarm->param.fDchargeSglTempDownLimitWarn = *pFVal;
				++pFVal;
				pBankAlarm->param.fBankSocUpLimitWarn = *pFVal;
				++pFVal;
				pBankAlarm->param.fBankSocDownLimitWarn = *pFVal;	
				++pFVal;

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}		
		}
		else if (GPRS_DATA_ALARM_PARAM == pGPRSHead->uCmdType)  // 读取告警参数
		{
			if (pGPRSHead->usDataLen == 4 * 12)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));

				pBankAlarm->param.fBankVolUpLimitAlarm = *pFVal;
				++pFVal;
				pBankAlarm->param.fBankVolDownLimitAlarm = *pFVal;
				++pFVal;
				pBankAlarm->param.fChargeCurUpLimitAlarm = *pFVal;
				++pFVal;
				pBankAlarm->param.fDischargeCurUpLimitAlarm = *pFVal;
				++pFVal;
				pBankAlarm->param.fSglVolUpLimitAlarm = *pFVal;
				++pFVal;
				pBankAlarm->param.fSglVolDownLimitAlarm = *pFVal;
				++pFVal;
				pBankAlarm->param.fChargeSglTempUpLimitAlarm = *pFVal;
				++pFVal;
				pBankAlarm->param.fChargeSglTempDownLimitAlarm = *pFVal;
				++pFVal;
				pBankAlarm->param.fDchargeSglTempUpLimitAlarm = *pFVal;
				++pFVal;
				pBankAlarm->param.fDchargeSglTempDownLimitAlarm = *pFVal;
				++pFVal;
				pBankAlarm->param.fBankSocUpLimitAlarm = *pFVal;
				++pFVal;
				pBankAlarm->param.fBankSocDownLimitAlarm = *pFVal;
				++pFVal;

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}		
		}
		else if (GPRS_DATA_PROT_PARAM == pGPRSHead->uCmdType)  // 读取保护参数
		{
			if (pGPRSHead->usDataLen == 4 * 12)
			{
				pFVal = (float*)(pData + sizeof(SGPRSHead));
			
				pBankAlarm->param.fBankVolUpLimitProt = *pFVal;
				++pFVal;
				pBankAlarm->param.fBankVolDownLimitProt = *pFVal;
				++pFVal;
				pBankAlarm->param.fChargeCurUpLimitProt = *pFVal;
				++pFVal;
				pBankAlarm->param.fDischargeCurUpLimitProt = *pFVal;
				++pFVal;
				pBankAlarm->param.fSglVolUpLimitProt = *pFVal;
				++pFVal;
				pBankAlarm->param.fSglVolDownLimitProt = *pFVal;
				++pFVal;
				pBankAlarm->param.fChargeSglTempUpLimitProt = *pFVal;
				++pFVal;
				pBankAlarm->param.fChargeSglTempDownLimitProt = *pFVal;
				++pFVal;
				pBankAlarm->param.fDchargeSglTempUpLimitProt = *pFVal;
				++pFVal;
				pBankAlarm->param.fDchargeSglTempDownLimitProt = *pFVal;	
				++pFVal;
				pBankAlarm->param.fBankSocUpLimitProt = *pFVal;
				++pFVal;
				pBankAlarm->param.fBankSocDownLimitProt = *pFVal;
				++pFVal;

				iRetVal = 0;
			}
			else
			{
				iRetVal = -4;
			}
		}
		else if (GPRS_CMD_SET_WARN_PARAM == pGPRSHead->uCmdType)  // 设置预警参数
		{
			if (pGPRSHead->usDataLen != 4)
			{
				iRetVal = -7;
			}
			else
			{
				iRetVal = 1;  // 不需要回复
			}
		}
		else if (GPRS_CMD_SET_ALARM_PARAM == pGPRSHead->uCmdType)  // 设置告警参数
		{
			if (pGPRSHead->usDataLen != 4)
			{
				iRetVal = -7;
			}
			else
			{
				iRetVal = 1;  // 不需要回复
			}
		}
		else if (GPRS_CMD_SET_PROT_PARAM == pGPRSHead->uCmdType)  // 设置保护参数
		{
			if (pGPRSHead->usDataLen != 4)
			{
				iRetVal = -7;
			}
			else
			{
				iRetVal = 1;  // 不需要回复
			}
		}
		else
		{
			iRetVal = -6;
		}
	}
	else
	{
		iRetVal = -3;
	}

	return iRetVal;
}

int CGPRSFactory::prepareExcepDatagram(char* pDataBuf, SGPRSHead* pGPRSHead, int iErrCode)
{
	if (!pDataBuf || !pGPRSHead)
	{
		return -1;
	}

	pGPRSHead->usDataLen = 4;
	memcpy(pDataBuf, (char*)pGPRSHead, sizeof(SGPRSHead));

	memset(pDataBuf + sizeof(SGPRSHead), 0, DATA_BUFFER_LENGHT - sizeof(SGPRSHead));

	memcpy(pDataBuf + sizeof(SGPRSHead), (char*)&iErrCode, sizeof(int));

	// CRC
	addCRC2Buffer((unsigned char *)pDataBuf, sizeof(SGPRSHead) + sizeof(int));

	return sizeof(int) + sizeof(SGPRSHead) + 2/*CRC*/;
}

// >= 0 OK 
// -1 参数错误 
// -2 长度不足
int CGPRSFactory::prepareDatagramStack(quint8 uCmdType, CBattStack* pBattStack, char* pDataBuf)
{
	if (GPRS_DATA_STACK != uCmdType || 0 == pBattStack || 0 == pDataBuf)
	{
		return -1;
	}

	// some variables  Head Data CRC
	SGPRSHead* pGPRSHead = 0;
	SNetBattStack* pNetBattStack = 0;

	if(sizeof(SGPRSHead) + sizeof(SNetBattStack) + 2 /*CRC*/ > DATA_BUFFER_LENGHT)
	{
		return -2;
	}

	// Head
	memset(pDataBuf, 0, DATA_BUFFER_LENGHT);

	pGPRSHead = (SGPRSHead*)(pDataBuf);
	pNetBattStack = (SNetBattStack*)(pDataBuf + sizeof(SGPRSHead));
	
	pGPRSHead->uCmdType = GPRS_DATA_STACK;
	pGPRSHead->uModbusAddrBg = 1;
	pGPRSHead->usDataLen = sizeof(SNetBattStack);

	// Data
	pNetBattStack->usStackState = pBattStack->usStackState;                  // 电池堆状态            1   
	pNetBattStack->usBranParalState1 = pBattStack->usBranParalState1;        // 第1支路并联状态     18  
	pNetBattStack->usBranParalState2 = pBattStack->usBranParalState2; 	     // 第2支路并联状态      19
	pNetBattStack->usBranParalState3 = pBattStack->usBranParalState3; 	     // 第3支路并联状态      20
	pNetBattStack->usBranParalState4 = pBattStack->usBranParalState4; 	     // 第4支路并联状态      21
	pNetBattStack->usBranState1 = pBattStack->usBranState1;                  // 第1支路电池堆状态    38         
	pNetBattStack->usBranState2 = pBattStack->usBranState2;                  // 第2支路电池堆状态    39          
	pNetBattStack->usBranState3 = pBattStack->usBranState3;                  // 第3支路电池堆状态    40         
	pNetBattStack->usBranState4 = pBattStack->usBranState4;                  // 第4支路电池堆状态    41

	pNetBattStack->usStackStateFull = pBattStack->usStackStateFull;		     // 电池堆充满           44
	pNetBattStack->usStackStateEmpty = pBattStack->usStackStateEmpty;		 // 电池堆放空           45
	pNetBattStack->usStackStateTempAlarm = pBattStack->usStackStateTempAlarm;// 电池堆温度告警       46
	pNetBattStack->usStackStateCurAlarm = pBattStack->usStackStateCurAlarm;  // 电池堆电流告警       47
	pNetBattStack->usStackStateAllOff = pBattStack->usStackStateAllOff;		 // 电池堆接触器全断开   48
	pNetBattStack->usStackStateFault = pBattStack->usStackStateFault;		 // 电池堆保护(PCS故障)  49

	pNetBattStack->fStackSoc = pBattStack->fStackSoc;                        // 电池堆SOC             2
	pNetBattStack->fStackSoh = pBattStack->fStackSoh; 	                     // 电池堆SOH             4
	pNetBattStack->fStackChargeEQ = pBattStack->fStackChargeEQ; 	         // 电池堆可充电量        6
	pNetBattStack->fStackDischargeEQ = pBattStack->fStackDischargeEQ;        // 电池堆可放电量        8
	pNetBattStack->fStackChargeEQAdd = pBattStack->fStackChargeEQAdd;        // 电池堆充电量累加值    10
	pNetBattStack->fStackDischargeEQAdd = pBattStack->fStackDischargeEQAdd;  // 电池堆放电量累加值    12

	pNetBattStack->fStackTotalVol = pBattStack->fStackTotalVol;              // 电池堆总电压         14  
	pNetBattStack->fStackTotalCur = pBattStack->fStackTotalCur; 	         // 电池堆总电流         16

	pNetBattStack->fBranSoc1 = pBattStack->fBranSoc1; 	             // 第1支路电池SOC       22
	pNetBattStack->fBranSoc2 = pBattStack->fBranSoc2; 	             // 第2支路电池SOC       24
	pNetBattStack->fBranSoc3 = pBattStack->fBranSoc3; 	             // 第3支路电池SOC       26
	pNetBattStack->fBranSoc4 = pBattStack->fBranSoc4; 	             // 第4支路电池SOC       28
	pNetBattStack->fBranCur1 = pBattStack->fBranCur1; 	             // 第1支路当前电流      30
	pNetBattStack->fBranCur2 = pBattStack->fBranCur2; 	             // 第2支路当前电流      32
	pNetBattStack->fBranCur3 = pBattStack->fBranCur3; 	             // 第3支路当前电流      34
	pNetBattStack->fBranCur4 = pBattStack->fBranCur4; 	             // 第4支路当前电流      36

	pNetBattStack->fSglTempCompVol = pBattStack->fSglTempCompVol;	 // 单体电池温度补偿电压 42	

	// CRC 
	addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + sizeof(SNetBattStack));

	return sizeof(SGPRSHead) + sizeof(SNetBattStack) + 2;
}

// >= 0 OK 
// -1 参数错误 
int CGPRSFactory::prepareDatagramBAComInputReg(quint8 uaBAIndex, quint16* pComInputRegData, char* pDataBuf)
{
	if (0 == pComInputRegData || 0 == pDataBuf || uaBAIndex < 1)
	{
		return -1;
	}

	// some variables  Head Data CRC
	int iLenVal = 0;	
	SGPRSHead* pGPRSHead = 0;

	// Head
	pGPRSHead = (SGPRSHead*)(pDataBuf);

	pGPRSHead->uCmdType = GPRS_DATA_BA_COM_INPUT_REG;
	pGPRSHead->uModbusAddrBg = uaBAIndex;
	pGPRSHead->usDataLen = 100 * 2;

	// Data
	memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)pComInputRegData, 100 * 2);

	// CRC
	addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 2);

	iLenVal = sizeof(SGPRSHead) + 2 * 100 + 2;

	return iLenVal;
}

// >= 0 OK 
// -1 参数错误 
// -2 长度不足
// -3 未知的消息类型
int CGPRSFactory::prepareDatagramCmd(quint8 uCmdType, CBattBank* pBattBank, char* pDataBuf, quint8 uModbusAddr)
{
	if (0 == pDataBuf)
	{
		return -1;
	}

	// some variables  Head  Data  CRC
	int iLenVal = 0;	
	SGPRSHead* pGPRSHead = 0;
	float* pFVal = 0;

	memset(pDataBuf, 0, DATA_BUFFER_LENGHT);
	pGPRSHead = (SGPRSHead*)pDataBuf;

	pGPRSHead->uCmdType = uCmdType;
	pGPRSHead->uModbusAddrBg = uModbusAddr;		

	switch (uCmdType)
	{
	case GPRS_CMD_REBOOT:               // 远程重启
	case GPRS_CMD_LAST_VERSION:         // 回退到上一版本

		pGPRSHead->usDataLen = 4;		

		// CRC
		addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 4);
		iLenVal = sizeof(SGPRSHead) + 4 + 2;
		break;
	case GPRS_CMD_SET_WARN_PARAM:       // 设置预警参数
	case GPRS_CMD_SET_ALARM_PARAM:      // 设置告警参数
	case GPRS_CMD_SET_PROT_PARAM:       // 设置保护参数
		if (pBattBank)
		{
			pGPRSHead->usDataLen = 12 * 4;

			// Data
			pFVal = (float*)(pDataBuf + sizeof(SGPRSHead));

			*pFVal = pBattBank->fCmdParam1; 
			++pFVal;
			*pFVal = pBattBank->fCmdParam2;
			++pFVal;
			*pFVal = pBattBank->fCmdParam3;
			++pFVal;
			*pFVal = pBattBank->fCmdParam4;
			++pFVal;
			*pFVal = pBattBank->fCmdParam5;
			++pFVal;
			*pFVal = pBattBank->fCmdParam6;
			++pFVal;
			*pFVal = pBattBank->fCmdParam7;
			++pFVal;
			*pFVal = pBattBank->fCmdParam8;
			++pFVal;
			*pFVal = pBattBank->fCmdParam9;
			++pFVal;
			*pFVal = pBattBank->fCmdParam10;
			++pFVal;
			*pFVal = pBattBank->fCmdParam11;
			++pFVal;
			*pFVal = pBattBank->fCmdParam12;
			++pFVal;

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 12 * 4);
			iLenVal = sizeof(SGPRSHead) + 4 * 12 + 2;
		}
		else
		{
			iLenVal = -1;
		}
		break;		
	default:
		iLenVal = -3;
		break;
	}

	return iLenVal;
}



// >= 0 OK 
// -1 参数错误 
// -2 长度不足
// -3 未知的消息类型
int CGPRSFactory::prepareDatagramBank(quint8 uCmdType, CBattBank* pBattBank, CBankAlarm* pBattAlarm, char* pDataBuf, quint8 uModbusAddr)
{
	if (0 == pBattBank || 0 == pDataBuf || 0 == pBattAlarm)
	{
		return -1;
	}

	// some variables  Head Data CRC
	int iLenVal = 0;	
	SGPRSHead* pGPRSHead = 0;
	SNetBattBankAlarm* pNetBattBankAlarm = 0;
	SNetBattBankSglOther* pNetBattBankSglOther = 0;
	float* pFVal = 0;

	memset(pDataBuf, 0, DATA_BUFFER_LENGHT);

	switch (uCmdType)
	{
	case GPRS_DATA_BANK_ALARM:       // 电池组数据 - 告警信息 
		if(sizeof(SGPRSHead) + sizeof(SNetBattBankAlarm) + 2 /*CRC*/ > DATA_BUFFER_LENGHT)
		{
			iLenVal = -2;
		}
		else
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);
			pNetBattBankAlarm = (SNetBattBankAlarm*)(pDataBuf + sizeof(SGPRSHead));

			pGPRSHead->uCmdType = GPRS_DATA_BANK_ALARM;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = sizeof(SNetBattBankAlarm);

			// Data
			pNetBattBankAlarm->aSglVolUpLimitAlarm = pBattBank->aSglVolUpLimitAlarm;		 // 单体电压过高告警       1
			pNetBattBankAlarm->aSglVolDownLimitAlarm = pBattBank->aSglVolDownLimitAlarm;	 // 单体电压过低告警       2	
			pNetBattBankAlarm->aSglTempUpLimitAlarm = pBattBank->aSglTempUpLimitAlarm;		 // 单体温度过高告警	   3
			pNetBattBankAlarm->aSglTempDownLimitAlarm = pBattBank->aSglTempDownLimitAlarm;	 // 单体温度过低告警	   4
			pNetBattBankAlarm->aSglVolUpLimitProt = pBattBank->aSglVolUpLimitProt;		     // 单体电压过高保护	   5
			pNetBattBankAlarm->aSglVolDownLimitProt = pBattBank->aSglVolDownLimitProt;		 // 单体电压过低保护	   6	
			pNetBattBankAlarm->aSglTempUpLimitProt = pBattBank->aSglTempUpLimitProt;		 // 单体温度过高保护	   7
			pNetBattBankAlarm->aSglTempDownLimitProt = pBattBank->aSglTempDownLimitProt;	 // 单体温度过低保护	   8	
			pNetBattBankAlarm->aBankVolUpLimitAlarm = pBattBank->aBankVolUpLimitAlarm;	     // 组电压过高告警         9
			pNetBattBankAlarm->aBankVolDownLimitAlarm = pBattBank->aBankVolDownLimitAlarm;   // 组电压过低告警         10
			pNetBattBankAlarm->aBankCurUpLimitAlarm = pBattBank->aBankCurUpLimitAlarm;		 // 组电流过高告警         11
			pNetBattBankAlarm->aEnvTempUpLimitAlarm = pBattBank->aEnvTempUpLimitAlarm;		 // 环境温度过高告警	   12
			pNetBattBankAlarm->aEnvTempDownLimitAlarm = pBattBank->aEnvTempDownLimitAlarm;	 // 环境温度过低告警	   13
			pNetBattBankAlarm->aBankSocDownLimitAlarm = pBattBank->aBankSocDownLimitAlarm;	 // SOC超下限告警		   14	
			pNetBattBankAlarm->aBankVolUpLimitProt = pBattBank->aBankVolUpLimitProt;		 // 组电压过高保护	       15
			pNetBattBankAlarm->aBankVolDownLimitProt = pBattBank->aBankVolDownLimitProt;	 // 组电压过低保护	       16	
			pNetBattBankAlarm->aBankCurUpLimitProt = pBattBank->aBankCurUpLimitProt;		 // 组电流过高保护		   17
			pNetBattBankAlarm->aEnvTempUpLimitProt = pBattBank->aEnvTempUpLimitProt;		 // 环境温度过高保护	   18
			pNetBattBankAlarm->aEnvTempDownLimitProt = pBattBank->aEnvTempDownLimitProt;	 // 环境温度过低保护	   19	
			pNetBattBankAlarm->aBankSocDownLimitProt = pBattBank->aBankSocDownLimitProt;	 // SOC超下限保护		   20
			pNetBattBankAlarm->aBMUConnFaultAlarm = pBattBank->aBMUConnFaultAlarm;		     // BMU设备连接故障告警	   21
			pNetBattBankAlarm->aBCMUConnFaultAlarm = pBattBank->aBCMUConnFaultAlarm;		 // BCMU设备连接故障告警   22	
			pNetBattBankAlarm->aSglVolUpLimitWarn = pBattBank->aSglVolUpLimitWarn;	         // 单体电压过高预警	   23
			pNetBattBankAlarm->aSglVolDownLimitWarn = pBattBank->aSglVolDownLimitWarn;	     // 单体电压过低预警	   24	
			pNetBattBankAlarm->aSglTempUpLimitWarn = pBattBank->aSglTempUpLimitWarn;	     // 单体温度过高预警	   25	
			pNetBattBankAlarm->aSglTempDownLimitWarn = pBattBank->aSglTempDownLimitWarn;	 // 单体温度过低预警       26
			pNetBattBankAlarm->aBankSocUpLimitAlarm = pBattBank->aBankSocUpLimitAlarm;	     // SOC超上限告警		   27
			pNetBattBankAlarm->aBankSocUpLimitProt = pBattBank->aBankSocUpLimitProt;		 // SOC超上限保护		   28
			pNetBattBankAlarm->aPosResDwonLimitProt = pBattBank->aPosResDwonLimitProt;		 // 正极绝缘内阻下限保护   29
			pNetBattBankAlarm->aNegResDwonLimitProt = pBattBank->aNegResDwonLimitProt;		 // 负极绝缘内阻下限保护   30
			pNetBattBankAlarm->aPackVolDiffUpLimitAlarm = pBattBank->aPackVolDiffUpLimitAlarm;	 // 箱压差上限告警         31
			pNetBattBankAlarm->aPackVolDiffUpLimitProt = pBattBank->aPackVolDiffUpLimitProt;	 // 箱压差上限保护         32
			pNetBattBankAlarm->aPackTempDiffUpLimitAlarm = pBattBank->aPackTempDiffUpLimitAlarm; // 箱温差上限告警         33
			pNetBattBankAlarm->aPackTempDiffUpLimitProt = pBattBank->aPackTempDiffUpLimitProt;	 // 箱温差上限保护         34	
			pNetBattBankAlarm->aBankVolUpLimitWarn = pBattBank->aBankVolUpLimitWarn;	         // 组电压过高预警         35
			pNetBattBankAlarm->aBankVolDownLimitWarn = pBattBank->aBankVolDownLimitWarn;         // 组电压过低预警         36
			pNetBattBankAlarm->aBankCurUpLimitWarn = pBattBank->aBankCurUpLimitWarn;		     // 组电流过高预警         37
			pNetBattBankAlarm->aEnvTempUpLimitWarn = pBattBank->aEnvTempUpLimitWarn;		     // 环境温度过高预警	   38
			pNetBattBankAlarm->aEnvTempDownLimitWarn = pBattBank->aEnvTempDownLimitWarn;	     // 环境温度过低预警	   39
			pNetBattBankAlarm->aBankSocUpLimitWarn = pBattBank->aBankSocUpLimitWarn;	         // SOC超上限预警		   40
			pNetBattBankAlarm->aBankSocDownLimitWarn = pBattBank->aBankSocDownLimitWarn;	     // SOC超下限预警		   41
			pNetBattBankAlarm->aPackVolDiffUpLimitWarn = pBattBank->aPackVolDiffUpLimitWarn;	 // 箱压差上限预警         42
			pNetBattBankAlarm->aPackTempDiffUpLimitWarn = pBattBank->aPackTempDiffUpLimitWarn;	 // 箱温差上限预警         43
			pNetBattBankAlarm->aBkwdSglVolReptProt = pBattBank->aBkwdSglVolReptProt;             // 落后电池电压多次保护   44
			pNetBattBankAlarm->aSglVolWireFault = pBattBank->aSglVolWireFault;                   // 单体电压采集线故障     45
			pNetBattBankAlarm->aBankVolWireFault = pBattBank->aBankVolWireFault;                 // 总压采集线故障         46
			pNetBattBankAlarm->aCurWireFault = pBattBank->aCurWireFault;                         // 电流采集线故障         47
			pNetBattBankAlarm->aTempWireBreak = pBattBank->aTempWireBreak;                       // 温度采集断线           48
			pNetBattBankAlarm->aTempShortCircuit = pBattBank->aTempShortCircuit;                 // 温度采集短路           49	
            pNetBattBankAlarm->aBMSFault = pBattBank->aBMSFault;                                 // BMS设备故障            50
			pNetBattBankAlarm->aBankCurAbnormity = pBattBank->aBankCurAbnormity;                 // 组电流异常             51	             
			pNetBattBankAlarm->aAlarmAvailable = pBattBank->aAlarmAvailable;                     // 告警信息待获取         52

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + sizeof(SNetBattBankAlarm));

			iLenVal = sizeof(SGPRSHead) + sizeof(SNetBattBankAlarm) + 2;
		}
		break;
	case GPRS_DATA_BANK_SGL_VOL1:    // 单体电压   1 - 100
		if (pBattBank->usCellNum > 1)
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_VOL1;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglVol[0]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}		
		break;
	case GPRS_DATA_BANK_SGL_VOL2:    // 单体电压 101 - 200
		if (pBattBank->usCellNum > 100)
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_VOL2;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglVol[100]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;
	case GPRS_DATA_BANK_SGL_VOL3:    // 单体电压 201 - 300
		if (pBattBank->usCellNum > 200)
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_VOL3;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglVol[200]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;
	case GPRS_DATA_BANK_SGL_VOL4:    // 单体电压 301 - 400
		if (pBattBank->usCellNum > 300)
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_VOL4;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglVol[300]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;
	case GPRS_DATA_BANK_SGL_TEMP1:   // 单体温度   1 - 100
		if (pBattBank->usTempNum > 1)
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_TEMP1;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglTemp[0]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;
	case GPRS_DATA_BANK_SGL_TEMP2:   // 单体温度 101 - 200
		if (pBattBank->usTempNum > 100)
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_TEMP2;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglTemp[100]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;
	case GPRS_DATA_BANK_SGL_TEMP3:   // 单体温度 201 - 300
		if (pBattBank->usTempNum > 200)
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_TEMP3;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglTemp[200]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;
	case GPRS_DATA_BANK_SGL_TEMP4:   // 单体温度 301 - 400
		if (pBattBank->usTempNum > 300)
			{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_TEMP4;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglTemp[300]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;
	case GPRS_DATA_BANK_SGL_SOC1:    // 单体Soc   1 - 100
		if (pBattBank->usCellNum > 1)
		{
		// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_SOC1;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglSoc[0]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;
	case GPRS_DATA_BANK_SGL_SOC2:    // 单体Soc 101 - 200
		if (pBattBank->usCellNum > 100)
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_SOC2;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglSoc[100]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;

	case GPRS_DATA_BANK_SGL_SOC3:    // 单体Soc 201 - 300
		if (pBattBank->usCellNum > 200)
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_SOC3;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglSoc[200]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;
	case GPRS_DATA_BANK_SGL_SOC4:    // 单体Soc 301 - 400
		if (pBattBank->usCellNum > 300)
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_SOC4;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglSoc[300]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;
	case GPRS_DATA_BANK_SGL_RES1:    // 单体内阻   1 - 100
		if (pBattBank->usCellNum > 1)
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_RES1;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglRes[0]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;
	case GPRS_DATA_BANK_SGL_RES2:    // 单体内阻 101 - 200
		if (pBattBank->usCellNum > 100)
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_RES2;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglRes[100]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;
	case GPRS_DATA_BANK_SGL_RES3:    // 单体内阻 201 - 300
		if (pBattBank->usCellNum > 200)
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_RES3;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglRes[200]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;
	case GPRS_DATA_BANK_SGL_RES4:    // 单体内阻 301 - 400
		if (pBattBank->usCellNum > 300)
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_RES4;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglRes[300]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;
	case GPRS_DATA_BANK_SGL_SOH1:    // 单体Soh   1 - 100
		if (pBattBank->usCellNum > 1)
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_SOH1;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglSoh[0]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;
	case GPRS_DATA_BANK_SGL_SOH2:    // 单体Soh 101 - 200
		if (pBattBank->usCellNum > 100)
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_SOH2;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglSoh[100]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;
	case GPRS_DATA_BANK_SGL_SOH3:    // 单体Soh 201 - 300
		if (pBattBank->usCellNum > 200)
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_SOH3;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglSoh[200]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;
	case GPRS_DATA_BANK_SGL_SOH4:    // 单体Soh 301 - 400
		if (pBattBank->usCellNum > 300)
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);

			pGPRSHead->uCmdType = GPRS_DATA_BANK_SGL_SOH4;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = 100 * 4;

			// Data
			memcpy((pDataBuf + sizeof(SGPRSHead)), (char*)&(pBattBank->fSglSoh[300]), 100 * 4);

			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 100 * 4);

			iLenVal = sizeof(SGPRSHead) + 4 * 100 + 2;
		}
		break;
	case GPRS_DATA_BANK_OTHER:       // 其他电池组数据
		if(sizeof(SGPRSHead) + sizeof(SNetBattBankSglOther) + 2 /*CRC*/ > DATA_BUFFER_LENGHT)
		{
			iLenVal = -2;
		}
		else
		{
			// Head
			pGPRSHead = (SGPRSHead*)(pDataBuf);
			pNetBattBankSglOther = (SNetBattBankSglOther*)(pDataBuf + sizeof(SGPRSHead));

			pGPRSHead->uCmdType = GPRS_DATA_BANK_OTHER;
			pGPRSHead->uModbusAddrBg = uModbusAddr;
			pGPRSHead->usDataLen = sizeof(SNetBattBankSglOther);

			// Data
			pNetBattBankSglOther->usSglMaxVolNo = pBattBank->usSglMaxVolNo;           // 单电池最大电压对应ID号 5758
			pNetBattBankSglOther->usSglMinVolNo = pBattBank->usSglMinVolNo;           // 单电池最小电压对应ID号 5761
			pNetBattBankSglOther->usSglMaxTempNo = pBattBank->usSglMaxTempNo;         // 单电池最大温度对应ID号 5764
			pNetBattBankSglOther->usSglMinTempNo = pBattBank->usSglMinTempNo;         // 单电池最小温度对应ID号 5767
			pNetBattBankSglOther->usSglMaxSocNo = pBattBank->usSglMaxSocNo;           // 单电池最大SOC对应ID号  5770	
			pNetBattBankSglOther->usSglMinSocNo = pBattBank->usSglMinSocNo;           // 单电池最小SOC对应ID号  5773
			pNetBattBankSglOther->usSglMaxSohNo = pBattBank->usSglMaxSohNo;           // 单电池最大SOH对应ID号  5776
			pNetBattBankSglOther->usSglMinSohNo = pBattBank->usSglMinSohNo;           // 单电池最小SOH对应ID号  5779
			pNetBattBankSglOther->usSglMaxResNo = pBattBank->usSglMaxResNo;           // 单电池最大内阻对应ID号 5786
			pNetBattBankSglOther->usSglMinResNo = pBattBank->usSglMinResNo;           // 单电池最小内阻对应ID号 5789
			pNetBattBankSglOther->usBCMUInput1 = pBattBank->usBCMUInput1;             // BCMU开关状态寄存器Input 1（直流接触器状态）		6000  1--闭合， 0--断开
			pNetBattBankSglOther->usBCMUInput2 = pBattBank->usBCMUInput2; 	          // BCMU Input 2（熔断器1状态）                    	6014
			pNetBattBankSglOther->usBCMUInput3 = pBattBank->usBCMUInput3; 	          // BCMU Input 3（熔断器2状态）	                    6016
			pNetBattBankSglOther->usBCMUInput4 = pBattBank->usBCMUInput4; 	          // BCMU Input 4（手动空开状态）		            6018			
			pNetBattBankSglOther->usBankState = pBattBank->usBankState; 
			pNetBattBankSglOther->usBankStatePCS = pBattBank->usBankStatePCS; 	      // 电池组状态（告知PCS）	                        6100  0x1111充满，0x2222放空，0x5555待机，0xaaaa跳机，0xbbbb正常

         	pNetBattBankSglOther->fSglAvgRes = pBattBank->fSglAvgRes;                 // 单体电池平均内阻	   5746
			pNetBattBankSglOther->fSglAvgSoh = pBattBank->fSglAvgSoh;                 // 单体电池SOH平均值      5748
			pNetBattBankSglOther->fSglAvgSoc = pBattBank->fSglAvgSoc; 	              // 单体电池SOC平均值      5750	
			pNetBattBankSglOther->fSglAvgTemp = pBattBank->fSglAvgTemp; 	          // 单体电池温度平均值     5752
			pNetBattBankSglOther->fSglAvgVol = pBattBank->fSglAvgVol;                 // 单体电池电压平均值     5754
			pNetBattBankSglOther->fSglMaxVol = pBattBank->fSglMaxVol;                 // 单电池电压最大值       5756
			pNetBattBankSglOther->fSglMinVol = pBattBank->fSglMinVol; 	              // 单电池电压最小值	   5759	
			pNetBattBankSglOther->fSglMaxTemp = pBattBank->fSglMaxTemp;               // 单电池温度最大值	   5762	
			pNetBattBankSglOther->fSglMinTemp = pBattBank->fSglMinTemp;               // 单电池温度最小值	   5765	
			pNetBattBankSglOther->fSglMaxSoc = pBattBank->fSglMaxSoc;                 // 单体SOC最大值		   5768	
			pNetBattBankSglOther->fSglMinSoc = pBattBank->fSglMinSoc;                 // 单体SOC最小值	       5771	
			pNetBattBankSglOther->fSglMaxSoh = pBattBank->fSglMaxSoh;                 // 单体SOH最大值	       5774	
			pNetBattBankSglOther->fSglMinSoh = pBattBank->fSglMinSoh;                 // 单体SOH最小值	       5777	
			pNetBattBankSglOther->fBankSoc = pBattBank->fBankSoc;                     // 组SOC		           5780
			pNetBattBankSglOther->fBankSoh = pBattBank->fBankSoh;                     // 组SOH	               5782
			pNetBattBankSglOther->fSglMaxRes = pBattBank->fSglMaxRes;                 // 单体内阻最大值	       5784	
			pNetBattBankSglOther->fSglMinRes = pBattBank->fSglMinRes;                 // 单体内阻最小值		   5787

			pNetBattBankSglOther->fBCMUBankVol = pBattBank->fBCMUBankVol; 	          // BCMU组端电压		                            6002
			pNetBattBankSglOther->fBCMUBankCur = pBattBank->fBCMUBankCur; 	          // BCMU组电流		                                6004
			pNetBattBankSglOther->fBCMUBankTemp = pBattBank->fBCMUBankTemp; 	      // BCMU环境温度		                            6006
			pNetBattBankSglOther->fBCMUPosRes = pBattBank->fBCMUPosRes; 	          // BCMU 正极绝缘电阻	                            6010
			pNetBattBankSglOther->fBCMUNegRes = pBattBank->fBCMUNegRes;               // BCMU 负极绝缘电阻	                         	6012

			pNetBattBankSglOther->fBankChargeEQ = pBattBank->fBankChargeEQ; 	      // 电池组可充电量                               	6200
			pNetBattBankSglOther->fBankDischargeEQ = pBattBank->fBankDischargeEQ; 	  // 电池组可放电量	                                6202                          
			pNetBattBankSglOther->fBankChargeEQAdd = pBattBank->fBankChargeEQAdd; 	         // 电池组累计充电电量	                            6216
			pNetBattBankSglOther->fBankDischargeEQAdd = pBattBank->fBankDischargeEQAdd;      // 电池组累计放电电量	                            6218
			pNetBattBankSglOther->fBankSglChargeEQAdd = pBattBank->fBankSglChargeEQAdd;   	 // 电池组单次累计充电电量	                        6220
			pNetBattBankSglOther->fBankSglDischargeEQAdd = pBattBank->fBankSglDischargeEQAdd;// 电池组单次累计放电电量	                        6222
			
			// CRC
			addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + sizeof(SNetBattBankSglOther));

			iLenVal = sizeof(SGPRSHead) + sizeof(SNetBattBankSglOther) + 2;
		}
		break;

	case GPRS_DATA_WARN_PARAM:    // 预警参数  		
		// Head
		pGPRSHead = (SGPRSHead*)(pDataBuf);

		pGPRSHead->uCmdType = GPRS_DATA_WARN_PARAM;
		pGPRSHead->uModbusAddrBg = uModbusAddr;
		pGPRSHead->usDataLen = 12 * 4;

		// Data
		pFVal = (float*)(pDataBuf + sizeof(SGPRSHead));

		*pFVal = pBattAlarm->param.fBankVolUpLimitWarn;
		++pFVal;
		*pFVal = pBattAlarm->param.fBankVolDownLimitWarn;
		++pFVal;
		*pFVal = pBattAlarm->param.fChargeCurUpLimitWarn;
		++pFVal;
		*pFVal = pBattAlarm->param.fDischargeCurUpLimitWarn;
		++pFVal;
		*pFVal = pBattAlarm->param.fSglVolUpLimitWarn;
		++pFVal;
		*pFVal = pBattAlarm->param.fSglVolDownLimitWarn;
		++pFVal;
		*pFVal = pBattAlarm->param.fChargeSglTempUpLimitWarn;
		++pFVal;
		*pFVal = pBattAlarm->param.fChargeSglTempDownLimitWarn;
		++pFVal;
		*pFVal = pBattAlarm->param.fDchargeSglTempUpLimitWarn;
		++pFVal;
		*pFVal = pBattAlarm->param.fDchargeSglTempDownLimitWarn;
		++pFVal;
		*pFVal = pBattAlarm->param.fBankSocUpLimitWarn;
		++pFVal;
		*pFVal = pBattAlarm->param.fBankSocDownLimitWarn;
		++pFVal;

		// CRC
		addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 12 * 4);
		iLenVal = sizeof(SGPRSHead) + 4 * 12 + 2;
		break;
	case GPRS_DATA_ALARM_PARAM:    // 告警参数  		
		// Head
		pGPRSHead = (SGPRSHead*)(pDataBuf);

		pGPRSHead->uCmdType = GPRS_DATA_ALARM_PARAM;
		pGPRSHead->uModbusAddrBg = uModbusAddr;
		pGPRSHead->usDataLen = 12 * 4;

		// Data
		pFVal = (float*)(pDataBuf + sizeof(SGPRSHead));

		*pFVal = pBattAlarm->param.fBankVolUpLimitAlarm;
		++pFVal;
		*pFVal = pBattAlarm->param.fBankVolDownLimitAlarm;
		++pFVal;
		*pFVal = pBattAlarm->param.fChargeCurUpLimitAlarm;
		++pFVal;
		*pFVal = pBattAlarm->param.fDischargeCurUpLimitAlarm;
		++pFVal;
		*pFVal = pBattAlarm->param.fSglVolUpLimitAlarm;
		++pFVal;
		*pFVal = pBattAlarm->param.fSglVolDownLimitAlarm;
		++pFVal;
		*pFVal = pBattAlarm->param.fChargeSglTempUpLimitAlarm;
		++pFVal;
		*pFVal = pBattAlarm->param.fChargeSglTempDownLimitAlarm;
		++pFVal;
		*pFVal = pBattAlarm->param.fDchargeSglTempUpLimitAlarm;
		++pFVal;
		*pFVal = pBattAlarm->param.fDchargeSglTempDownLimitAlarm;
		++pFVal;
		*pFVal = pBattAlarm->param.fBankSocUpLimitAlarm;
		++pFVal;
		*pFVal = pBattAlarm->param.fBankSocDownLimitAlarm;
		++pFVal;
		// CRC
		addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 12 * 4);
		iLenVal = sizeof(SGPRSHead) + 4 * 12 + 2;
		break;
	case GPRS_DATA_PROT_PARAM:    // 保护参数  		
		// Head
		pGPRSHead = (SGPRSHead*)(pDataBuf);

		pGPRSHead->uCmdType = GPRS_DATA_PROT_PARAM;
		pGPRSHead->uModbusAddrBg = uModbusAddr;
		pGPRSHead->usDataLen = 12 * 4;

		// Data
		pFVal = (float*)(pDataBuf + sizeof(SGPRSHead));
		*pFVal = pBattAlarm->param.fBankVolUpLimitProt;
		++pFVal;
		*pFVal = pBattAlarm->param.fBankVolDownLimitProt;
		++pFVal;
		*pFVal = pBattAlarm->param.fChargeCurUpLimitProt;
		++pFVal;
		*pFVal = pBattAlarm->param.fDischargeCurUpLimitProt;
		++pFVal;
		*pFVal = pBattAlarm->param.fSglVolUpLimitProt;
		++pFVal;
		*pFVal = pBattAlarm->param.fSglVolDownLimitProt;
		++pFVal;
		*pFVal = pBattAlarm->param.fChargeSglTempUpLimitProt;
		++pFVal;
		*pFVal = pBattAlarm->param.fChargeSglTempDownLimitProt;
		++pFVal;
		*pFVal = pBattAlarm->param.fDchargeSglTempUpLimitProt;
		++pFVal;
		*pFVal = pBattAlarm->param.fDchargeSglTempDownLimitProt;
		++pFVal;
		*pFVal = pBattAlarm->param.fBankSocUpLimitProt;
		++pFVal;
		*pFVal = pBattAlarm->param.fBankSocDownLimitProt;
		++pFVal;	

		// CRC
		addCRC2Buffer((uchar *)pDataBuf, sizeof(SGPRSHead) + 12 * 4);
		iLenVal = sizeof(SGPRSHead) + 4 * 12 + 2;			
		break;
	default:
		iLenVal = -3;
		break;
	}

	return iLenVal;
}

int CGPRSFactory::addParam2Buffer(char* pData, quint32 uiParamVal)
{
	*pData = (char)(uiParamVal >> 24);

	++pData;
	*pData = (char)(uiParamVal >> 16);

	++pData;
	*pData = (char)(uiParamVal >> 8);

	++pData;
	*pData = (char)(uiParamVal);

	return  0;
}

int CGPRSFactory::addParam2Buffer(char* pData, quint16 uiParamVal)
{
	*pData = (char)(uiParamVal >> 8);

	++pData;
	*pData = (char)(uiParamVal);

	return  0;
}

int CGPRSFactory::addParam2Buffer(char* pData, float fParamVal)
{
	char aTemp[4];
	memcpy(aTemp, &fParamVal, 4);

	*pData = aTemp[3];
	++pData;
	*pData = aTemp[2];
	++pData;
	*pData = aTemp[1];
	++pData;
	*pData = aTemp[0];

	return 0;
}

int CGPRSFactory::getParamFromBuffer(char* pData, quint16& uiParamVal)
{
	uiParamVal = (unsigned char)*pData;
	uiParamVal = uiParamVal * 256;

	++pData;

	uiParamVal += (unsigned char)*pData;

	return 0;
}

int CGPRSFactory::getParamFromBuffer(char* pData, quint32& uiParamVal)
{
	uiParamVal = (unsigned char)*pData;

	uiParamVal = uiParamVal * 256;
	++pData;
	uiParamVal += (unsigned char)*pData;

	uiParamVal = uiParamVal * 256;
	++pData;
	uiParamVal += (unsigned char)*pData;

	uiParamVal = uiParamVal * 256;
	++pData;
	uiParamVal += (unsigned char)*pData;

	return 0;
}


int CGPRSFactory::getParamFromBuffer(char* pData, float& fParamVal)
{
	char aTemp[4];
	aTemp[3] = *pData;
	++pData;
	aTemp[2] = *pData;
	++pData;
	aTemp[1] = *pData;
	++pData;
	aTemp[0] = *pData;

	memcpy(&fParamVal, aTemp, 4);
	return 0;
}

int CGPRSFactory::addCRC2Buffer(uchar *pData, int iLen)
{
	quint16 usCRC = CRC16(pData, iLen);

	return addParam2Buffer((char*)(pData + iLen), usCRC);
}

// 计算CRC校验
//********************************************************************
// FUCTION: CRC16 —— CRC16校验生成函数
// VERSION: 1	(2010-9-27 14:34:01)
// INPUT:	dat_buf 待校验数值缓存区
// 			dat_len 待校验数值缓存区的长度
// OUTPUT:	CRC16校验值
// EXAMPLE:
// 输入数据 dat_buf指向的数组为： 0x33, 0x78, 0x98, 0x60, 校验长度为4
// 则输出为：高字节0x3B 低字节0x78
// NOTE：	参考技术标准文件《CRC校验原理》		
//********************************************************************
quint16 CGPRSFactory::CRC16(uchar *dat_buf, quint16 dat_len)
{
	uchar j, r1, r2, r3, r4;
	quint16 i;
	
	r1 = dat_buf[0];
	r2 = dat_buf[1];

	for (i = 2; i < dat_len + 2; i++)
	{
		if (i < dat_len)
		{
			r3 = dat_buf[i];		
		}
		else
		{
			r3 = 0;
		}

		for (j = 0; j < 8; j++)
		{
			r4 = r1;
			r1 = (r1 << 1);
			if ((r2 & 0x80) != 0) r1++;
			r2 = r2 << 1;
			if ((r3 & 0x80) != 0) r2++;
			r3 = (r3 << 1);
			if ((r4 & 0x80) != 0)
			{
				r1 = r1 ^ 0x10;
				r2 = r2 ^ 0x21;
			}
		}
	}
	return (r1 * 0x100 + r2);
}

